package com.corner.model.leancloud.helper;

import com.alibaba.fastjson.JSONObject;
import com.corner.model.cornerservice.service.Service;
import com.corner.model.cornerservice.service.exception.BaseException;
import com.corner.model.leancloud.base.BaseBeanHelper;

import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * 更新相关业务
 *
 * @author Administrator
 */
public class UpdateHelper extends BaseBeanHelper {
    private static final String UPDATE_APP_URL = "appUrl";
    private static final String VERSION = "version";

    /**
     * 获取最新版本
     */
    public static Observable<JSONObject> getUpdate() {

        return Observable.create(new Observable.OnSubscribe<JSONObject>() {
            @Override
            public void call(Subscriber<? super JSONObject> subscriber) {
                try {
                    JSONObject update = Service.getInstance().updateService.getUpdate();
                    subscriber.onNext(update);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
            }
        }).subscribeOn(Schedulers.io());

    }

    /**
     * 获取最新app版本
     *
     * @param avObject
     * @return
     */
    public static String getUpdateVersion(JSONObject avObject) {
        return avObject.getString(VERSION);
    }

    /**
     * 获取最新appAVFile
     *
     * @param avObject
     * @return
     */
    public static String getUpdateAppUrl(JSONObject avObject) {
        return avObject.getString(UPDATE_APP_URL);
    }
}
